﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using Walson;

namespace Walson.Form.Common
{
    public partial class GpsState : UserControl
    {     

        private GpsStates _GpsState = GpsStates.Off;
        public GpsStates State
        {
            get
            {
                return _GpsState;
            }
            set
            {
                if (_GpsState != value)
                {
                    _GpsState = value;

                    if (_GpsState == GpsStates.Off)
                    {
                        _Timer.Enabled = false;
                        this.BackgroundImage = _GpsOff;
                    }
                    else if (_GpsState == GpsStates.On)
                    {
                        _Timer.Enabled = false;
                        this.BackgroundImage = _GpsOn2;
                    }
                    else if (_GpsState == GpsStates.Searching)
                    {
                        _Timer.Enabled = true;
                    }
                }
            }
        }

        private Bitmap _GpsOff, _GpsOn0, _GpsOn1, _GpsOn2;
        private Timer _Timer;
        private int _TimerTick = 0;
        private void LoadResource()
        {
            Assembly myAssembly = Assembly.GetExecutingAssembly();
            Stream myStream1 = myAssembly.GetManifestResourceStream("Walson.Resources.GpsOff.png");
            _GpsOff = new Bitmap(myStream1);

            Stream myStream2 = myAssembly.GetManifestResourceStream("Walson.Resources.GpsSearching0.png");
            _GpsOn0 = new Bitmap(myStream2);

            Stream myStream3 = myAssembly.GetManifestResourceStream("Walson.Resources.GpsSearching1.png");
            _GpsOn1 = new Bitmap(myStream3);

            Stream myStream4 = myAssembly.GetManifestResourceStream("Walson.Resources.GpsSearching2.png");
            _GpsOn2 = new Bitmap(myStream4);
        }

        public GpsState()
        {
            LoadResource();
            _Timer = new Timer();
            _Timer.Interval = 500;
            _Timer.Tick += delegate
            {
                switch (_TimerTick)
                {
                    case 0:
                        this.BackgroundImage = _GpsOn0;
                        break;
                    case 1:
                        this.BackgroundImage = _GpsOn1;
                        break;
                    case 2:
                        this.BackgroundImage = _GpsOn2;
                        break;
                }

                _TimerTick++;
                if (_TimerTick == 3)
                    _TimerTick = 0;
            };

            InitializeComponent();
            this.DoubleBuffered = true;

            this.BackgroundImage = _GpsOff;
        }
    }
}
